<?php
namespace alicloud;

use think\Exception;

class IdNumberCheck{
    public static function check($name, $idNumber) {
        $host = "https://swidverify.market.alicloudapi.com";
        $path = "/verify/identity";
        $method = "POST";
        $appcode = "ff0e889bcc7143ec8a2b1903cc067d06";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys = "name={$name}&id_number={$idNumber}";
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$" . $host, "https://")) {

            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $str = curl_exec($curl);
        if(empty($str)){
            throw new \Exception("实名认证接口调用出错！");
        }
        $result = json_decode($str);
        print_r($result);
        if($result->charge == 1){
            if(isset($result->result->checkresult)){
                //echo 'aaa';exit;
                if($result->result->checkresult == 1){
                    return true;
                }else{
                    $msg = $result->result->messge ?? '其它错误';
                    throw new \Exception($msg);
                }
            }
            throw new \Exception("实名认证，未知错误");
        }
        if($result->charge == 2){
            $msg = str_replace('请求参数错误:', '', $result->result_msg);
            throw new \Exception($msg);
        }
    }
}

?>